/**
 * @Description 基础框架服务调用实例-全文检索服务
 * @Author  guoyanfei
 * @date 2018/03/28
 */
var searchServices = function () {
    //检索数据
    var getsearch = {
        getData: function (pageindex, isPage) {
            var searchUrl = serviceUrl + "/business/searchServer/seachDocData";
            $.ajax({
                url: searchUrl,
                type: 'get',
                async: false,
                data: {
                    serviceID: serviceId,
                    filenames: $("#searchFilename").val(),
                    keyword: $("#searchParm").val(),
                    pageNO: pageindex,
                    pageSize: $("#searchNum").val(),
                    includesContext: true
                },
                success: function (res) {
                    if (res.result == 0) {
                        if (isPage) {
                            //将查询结果分页
                            if (res.data.total >= 0) {
                                pages.generatePages(res.data.total);
                            }
                        }
                        $("#infomess").html(res.data.description + ",共约" + res.data.total + "条记录");
                        var result = JSON.stringify(res.data.rows)
                            .replace(/},/g, "},<br/>");
                        $("#info").html(result);
                    } else {
                        $("#info").html(JSON.stringify(res));
                    }
                },
                error: function () {
                    dialog.modal('hide');
                    bootbox.alert("检索数据失败!");
                }
            });
        },
        getkeyword: function () {
            //查询热词
            $.ajax({
                type: 'get',
                url: serviceUrl + '/business/searchServer/seachHotword',
                data: {serviceID: serviceId},
                async: false,
                success: function (res) {
                    $("#kwul").html("");
                    $.each(res, function (i, e) {
                        $("#kwul").append("<li data-ve='" + e + "'>" + e + "</li>");
                    })
                }
            });
        },
        init: function () {
            $("#btn_search").click(function () {
                getsearch.getData(1, true);
                $("#keyword").css("display", "none");
            });
            $("#searchParm").focus(function () {
                getsearch.getkeyword();
                $("#keyword").css("display", "block");
            });

            $("#kwul").on('click', 'li', function () {
                $("#searchParm").val($(this).attr("data-ve"));
                $("#keyword").css("display", "none");
            });
        }
    };

    //分页
    var pages = {
        generatePages: function (total) {//生成分页按钮
            var resTotal = 1;
            var pagesize = 20.0;
            //当查询结果总条数大于0时，分页数由每页条数决定，当total为0时，分页为1
            if (total > 0) {
                resTotal = total;
            }
            if (!$("#searchNum").val() == "") {
                pagesize = $("#searchNum").val()
            }
            $("#page").html("");
            $('#page').jqPaginator({
                visiblePages: 10,
                totalPages: Math.ceil(resTotal / pagesize),
                currentPage: 1,
                first: '<li class="first"><a href="javascript:;">首页</a></li>',
                prev: '<li class="prev"><a href="javascript:;">上一页</a></li>',
                next: '<li class="next"><a href="javascript:;">下一页</a></li>',
                last: '<li class="last"><a href="javascript:;">末页</a></li>',
                page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',

                onPageChange: function (num, type) {
                    getsearch.getData(num, false);
                }
            });
        }
    };

    return {
        init: function () {
            getsearch.init();
        }
    }
}();

//服务初始化
$(function () {
    $("#sp_seviceUrl").append("服务地址：" + serviceUrl + "/business/searchServer/seachDocData");
    $("#sp_seviceUrl").append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;服务ID：" + serviceId);
    $("#show_serviceUrl").append(serviceUrl);
    $("#show_serviceId").append(serviceId);
    searchServices.init();
})